package com.example.demo.t2;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.lang.reflect.Array;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.AsynchronousFileChannel;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.Arrays;
import java.util.Random;
import java.util.concurrent.Future;

public class T2_11 {

    public static void main(String[] args) throws Exception {
        var raw = "长坂坡杀气生，横刀立马。";
        var charset = StandardCharsets.UTF_8;
        byte[] bytes = charset.encode(raw).array();
        byte[] bytes1 = Arrays.copyOfRange(bytes, 3, 12);

        var bBuff = ByteBuffer.allocate(12);
        var cBuff = CharBuffer.allocate(12);

        bBuff.put(bytes1);
        bBuff.flip();

        charset.newDecoder().decode(bBuff, cBuff, false);
        cBuff.flip();

//        char[] chars = cBuff.array();
        var chars = new char[cBuff.length()];
        cBuff.get(chars);
        System.out.println(new String(chars));

        System.out.format("%d - %d - %d - %d - %d", bBuff.limit(), bBuff.position(), cBuff.limit(), cBuff.position(), chars.length);

    }
}

